package com.toprs.base.service

import com.toprs.base.annotation.PageQuery
import com.toprs.base.config.GlobalCurrent
import com.toprs.base.entity.Log
import com.toprs.base.mapper.LogDynamicSqlSupport
import com.toprs.base.mapper.LogMapper
import com.toprs.base.mapper.delete
import com.toprs.base.mapper.query
import com.toprs.base.query.LogQuery
import com.toprs.base.result.PageResult
import com.toprs.base.utils.FileUtil
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.io.File

@Service
class LogService(val logMapper: LogMapper) {

    //    @Value("\${toprs.dataPath}")
    val dataPath by lazy {
        GlobalCurrent.dataPath
    }

    @PageQuery
    fun queryLog(logQuery: LogQuery): PageResult<Log> {
        val list = logMapper.query(logQuery) as com.github.pagehelper.Page<Log>
        return PageResult.processPage(list)
    }

    fun deleteLogs(logIds: List<Int>) {
        logMapper.delete {
            where {
                LogDynamicSqlSupport.id.isIn(logIds)
            }
        }
    }

    fun exportExcel(logQuery: LogQuery): String {
        val list = logMapper.query(logQuery)
        val title = when (logQuery.logType) {
            "1" -> "登录日志"
            "2" -> "操作日志"
            "3" -> "错误日志"
            else -> ""
        }

        val path = FileUtil.exportExcelLog(list, title, dataPath)
        val res = FileUtil.file2base64(path)
        File(path).delete()
        return res
    }
}